package xml.parser;

import org.w3c.dom.*;
import device.IXmlConfig;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

/**
 * Created by vondoom on 31.05.2014.
 */
public class DomParser {

    Boolean search = false;
    public DomParser() {
        /*try
        {
            File xmlFile = new File("C:\\sensors.xml");
            DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
            Document document = documentBuilder.parse(xmlFile);
            document.getDocumentElement().normalize();

            System.out.println("Корневой элемент: " + document.getDocumentElement().getNodeName());
            NodeList nodeList = document.getElementsByTagName(document.getDocumentElement().getChildNodes().item(1).getNodeName());
            //System.out.println("find element: " + );
            System.out.println("--------------------");
            for(int tmp = 0; tmp < nodeList.getLength(); tmp++)
            {
                Node node = nodeList.item(tmp);
                //get id
                System.out.println("device #" + node.getNodeName()+" "+ tmp+":"+node.getAttributes().item(0).getNodeName()+"="+node.getAttributes().item(0).getTextContent());
                //get type
                System.out.println("device #" + node.getNodeName()+" "+ tmp+":"+node.getAttributes().item(1).getNodeName()+"="+node.getAttributes().item(1).getTextContent());
                if(node.getNodeType() == Node.ELEMENT_NODE)
                {
                    Element element = (Element)node;
                    System.out.println("min: " + element.getElementsByTagName("min").item(0).getChildNodes().item(0).getNodeValue());
                    System.out.println("max: " + element.getElementsByTagName("max").item(0).getChildNodes().item(0).getNodeValue());
                }
            }
        }
        catch (Exception e)
        {
            System.out.println(e.getLocalizedMessage());
            e.printStackTrace();
        }*/
    }

    public void initSensorFromXml(IXmlConfig sensor){
        if(sensor.getIdFromXml() != null){
            try
            {
                File xmlFile = new File("C:\\sensors.xml");
                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                Document document = documentBuilder.parse(xmlFile);
                document.getDocumentElement().normalize();

                //System.out.println("Корневой элемент: " + document.getDocumentElement().getNodeName());
                NodeList nodeList = document.getElementsByTagName(document.getDocumentElement().getChildNodes().item(1).getNodeName());
                //System.out.println("find element: " + );
                System.out.println("--------------------");
                for(int tmp = 0; tmp < nodeList.getLength(); tmp++)
                {
                    Node node = nodeList.item(tmp);
                    //get id
                    if(sensor.getIdFromXml().equals(node.getAttributes().item(0).getTextContent()) &&
                            node.getNodeType() == Node.ELEMENT_NODE) {
                        search = true;
                        Element element = (Element) node;
                        sensor.initFromXml(element);
                    }
                }
                if(search){
                    search = false;
                }else {
                    System.out.println("--- "+sensor.getIdFromXml()+" not found ---");
                }
            }
            catch (Exception e)
            {
                System.out.println(e.getLocalizedMessage());
                e.printStackTrace();
            }
        } else {
            System.out.println("--- xml id null ---");
        }
    }
}
